<template>
    <entity-manager apiPrefix="/manager/gateway" :fields="fields" ref="entityManagerRef" operationsWidth="180px">
        <template #prefixButtons="{ row }">
            <el-link type="primary" class="mr-2" @click="publish(row)">{{ row.status == 1 ? '重启' : '启动' }}</el-link>
            <el-link type="danger" class="mr-2" @click="stop(row)" v-if="row.status == 1">停止</el-link>
        </template>
    </entity-manager>
</template>

<script setup>
    import * as _ from 'lodash'
    import https from '@/utils/https'
    import { ElMessage, ElMessageBox } from 'element-plus'

    const fields = [
        { label: '网关编码', prop: 'code', required: true, width: '200px', unique: true },
        { label: '网关名称', prop: 'name', required: true },
        { label: '网关协议', prop: 'protocol', required: true, default: 'mqtt', width: '100px', type: 'radioGroup', options: [{ label: 'MQTT', value: 'mqtt' }] },
        { label: '网关状态', prop: 'status', needNew: false, width: '100px' },
        { label: '服务器IP', prop: 'connectionInfo.host', required: true, show: false },
        { label: '服务器端口', prop: 'connectionInfo.port', required: true, show: false, default: 1883 },
        { label: '连接用户', prop: 'connectionInfo.username', required: false, show: false },
        { label: '连接密码', prop: 'connectionInfo.password', required: false, show: false },
        { label: '备注', prop: 'remark', type: 'textarea' },
        { label: '创建时间', prop: 'createTime', width: '160px', needNew: false },
    ]
    const entityManagerRef = ref()

    function publish(row) {
        https.get('/manager/gateway/publish', { id: row.id }).then(resp => {
            ElMessage.success('操作成功')
            row.status = 1
        })
    }

    function stop(row) {
        ElMessageBox.confirm('确定停止当前网关？').then(() => {
            https.get('/manager/gateway/offline', { id: row.id }).then(() => {
                ElMessage.success('操作成功')
                row.status = 2
            })
        })
    }
</script>